package com.mindskip.xzs.controller.admin;

import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.Question;
import com.mindskip.xzs.domain.TextContent;
import com.mindskip.xzs.domain.question.QuestionObject;
import com.mindskip.xzs.service.ExamPaperService;
import com.mindskip.xzs.service.QuestionService;
import com.mindskip.xzs.service.TextContentService;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.utility.HtmlUtil;
import com.mindskip.xzs.utility.JsonUtil;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamResponseVM;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.question.QuestionPageRequestVM;
import com.mindskip.xzs.viewmodel.admin.question.QuestionResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController("AdminExamPaperController")
@RequestMapping(value = "/api/admin/exam/paper")
public class ExamPaperController extends BaseApiController {

    private final ExamPaperService examPaperService;
    private final QuestionService questionService;
    private final TextContentService textContentService;

    @Autowired
    public ExamPaperController(ExamPaperService examPaperService, QuestionService questionService, TextContentService textContentService) {
        this.examPaperService = examPaperService;
        this.questionService = questionService;
        this.textContentService = textContentService;
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamResponseVM>> pageList(@RequestBody ExamPaperPageRequestVM model) {
        PageInfo<ExamPaper> pageInfo = examPaperService.page(model);
        PageInfo<ExamResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamResponseVM vm = modelMapper.map(e, ExamResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(page);
    }



    @RequestMapping(value = "/taskExamPage", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamResponseVM>> taskExamPageList(@RequestBody ExamPaperPageRequestVM model) {
        PageInfo<ExamPaper> pageInfo = examPaperService.taskExamPage(model);
        PageInfo<ExamResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamResponseVM vm = modelMapper.map(e, ExamResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(page);
    }



    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> edit(@RequestBody @Valid ExamPaperEditRequestVM model) {
        if(null == model.getLevel()){
            model.setLevel(1);
        }
        model = this.getAutoExamPaper(model);
        ExamPaper examPaper = examPaperService.savePaperFromVM(model, getCurrentUser());
        ExamPaperEditRequestVM newVM = examPaperService.examPaperToVM(examPaper.getId());
        return RestResponse.ok(newVM);
    }

    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id) {
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        ExamPaper examPaper = examPaperService.selectById(id);
        examPaper.setDeleted(true);
        examPaperService.updateByIdFilter(examPaper);
        return RestResponse.ok();
    }

    private ExamPaperEditRequestVM getAutoExamPaper(ExamPaperEditRequestVM model){
        if (model.getExamPaper() == 1) {
            List<ExamPaperTitleItemVM> titleItems = new ArrayList<>();
            ExamPaperTitleItemVM examPaperTitleItemVM1 = new ExamPaperTitleItemVM();
            examPaperTitleItemVM1.setName("单选题");
            QuestionPageRequestVM questionPageRequestVM1 = new QuestionPageRequestVM();
            questionPageRequestVM1.setQuestionType(1);
            questionPageRequestVM1.setSubjectId(model.getSubjectId());
            questionPageRequestVM1.setPageIndex(1);
            questionPageRequestVM1.setPageSize(60);
            List<Question> questionList = questionService.selectQuestion(questionPageRequestVM1);
            List<QuestionEditRequestVM> questionItems = new ArrayList<>();
            for(Question question : questionList){
                QuestionEditRequestVM vm = modelMapper.map(question, QuestionEditRequestVM.class);
                vm.setScore(ExamUtil.scoreToVM(question.getScore()));
                questionItems.add(vm);
            }
            examPaperTitleItemVM1.setQuestionItems(questionItems);
            titleItems.add(examPaperTitleItemVM1);

            ExamPaperTitleItemVM examPaperTitleItemVM2 = new ExamPaperTitleItemVM();
            examPaperTitleItemVM2.setName("多选题");
            QuestionPageRequestVM questionPageRequestVM2 = new QuestionPageRequestVM();
            questionPageRequestVM2.setQuestionType(2);
            questionPageRequestVM2.setSubjectId(model.getSubjectId());
            questionPageRequestVM2.setPageIndex(1);
            questionPageRequestVM2.setPageSize(20);
            List<Question> questionList2 = questionService.selectQuestion(questionPageRequestVM2);
            List<QuestionEditRequestVM> questionItems2 = new ArrayList<>();
            for(Question question : questionList2){
                QuestionEditRequestVM vm = modelMapper.map(question, QuestionEditRequestVM.class);
                vm.setScore(ExamUtil.scoreToVM(question.getScore()));
                questionItems2.add(vm);
            }
            examPaperTitleItemVM2.setQuestionItems(questionItems2);
            titleItems.add(examPaperTitleItemVM2);

            ExamPaperTitleItemVM examPaperTitleItemVM3 = new ExamPaperTitleItemVM();
            examPaperTitleItemVM3.setName("判断题");
            questionPageRequestVM2.setQuestionType(3);
            List<Question> questionList3 = questionService.selectQuestion(questionPageRequestVM2);
            List<QuestionEditRequestVM> questionItems3 = new ArrayList<>();
            for(Question question : questionList3){
                QuestionEditRequestVM vm = modelMapper.map(question, QuestionEditRequestVM.class);
                vm.setScore(ExamUtil.scoreToVM(question.getScore()));
                questionItems3.add(vm);
            }
            examPaperTitleItemVM3.setQuestionItems(questionItems3);
            titleItems.add(examPaperTitleItemVM3);
            model.setTitleItems(titleItems);
        }
        return model;
    }
}
